package com.demo.key.pa

import android.animation.ValueAnimator
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.KeyEvent
import android.view.animation.LinearInterpolator
import androidx.core.animation.doOnEnd
import com.blankj.utilcode.util.ActivityUtils
import com.demo.key.R
import com.demo.key.load_admob.AdmobKeyType
import com.demo.key.load_admob.LoadAdmob0822Manager
import com.demo.key.show_admob.ShowFullAd0822
import kotlinx.android.synthetic.main.activity_main.*

class Main0822Pa : Abs0822Page() {
    private var an0822: ValueAnimator?=null
    private val showOpenAd by lazy { ShowFullAd0822(this,AdmobKeyType.OPEN){ home0822() } }

    override fun layoutId(): Int = R.layout.activity_main

    override fun view() {
        LoadAdmob0822Manager.logic(AdmobKeyType.OPEN)
        LoadAdmob0822Manager.logic(AdmobKeyType.CONNECT)
        LoadAdmob0822Manager.logic(AdmobKeyType.HOME)
        LoadAdmob0822Manager.logic(AdmobKeyType.RESULT)

        an0822 = ValueAnimator.ofInt(0, 100).apply {
            duration=10000L
            interpolator = LinearInterpolator()
            addUpdateListener {
                val pro = it.animatedValue as Int
                progress_bar.progress = pro
                val d = (10 * (pro / 100.0F)).toInt()
                if (d in 2..9){
                    if (showOpenAd.loadFullAdFinish()){
                        an0822?.removeAllUpdateListeners()
                        an0822?.cancel()
                        an0822=null
                        progress_bar.progress = 100
                        showOpenAd.show()
                    }
                }else if (d>=10){
                    home0822()
                }
            }
            start()
        }
    }


    private fun home0822(){
        if (!ActivityUtils.isActivityExistsInStack(Home0822Pa::class.java)){
            startActivity(Intent(this,Home0822Pa::class.java))
        }
        finish()
    }


    override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode== KeyEvent.KEYCODE_BACK){
            return true
        }
        return false
    }

    override fun onDestroy() {
        super.onDestroy()
        an0822?.removeAllUpdateListeners()
        an0822?.cancel()
        an0822=null
    }
}